package com.example.android3;


import android.os.Bundle;

import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
public class RadioButton extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private RadioGroup radioGroup;
    private TextView textView;
    private TextView hobby;
    private  String hobbys;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        CheckBox shuttlecock=findViewById(R.id.like_shuttlecock);
        CheckBox basketball=findViewById(R.id.like_basketball);
        CheckBox pingpong=findViewById(R.id.like_pingpong);
        shuttlecock.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        pingpong.setOnCheckedChangeListener(this);
        hobby=findViewById(R.id.hobby);
        hobbys=new String();
        radioGroup=findViewById(R.id.rdg);
        textView=findViewById(R.id.tv);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==R.id.rbtn) {
                    textView.setText("你的性别是男");
                } else
                    textView.setText("你的性别是女");

            }
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String motion=buttonView.getText().toString();
        if(isChecked){
            if(!hobbys.contains(motion)){
                hobbys=hobbys+motion;
                hobby.setText(hobbys);
            }
        }else{
            if(hobbys.contains(motion)){
                hobbys=hobbys.replace(motion,"");
                hobby.setText(hobbys);
            }
        }
    }
}